<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img :src="require('@/assets/logo.png')" alt="">
      </div>
      <el-form label-width="0px" class="login_in" ref="loginForm" :model="loginForm">
        <el-form-item prop="userId">
          <el-input type="text" prefix-icon="el-icon-user" v-model="loginForm.userId"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" prefix-icon="el-icon-thumb" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
          <el-button @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {post} from "@/util/axios";

export default {
  name: "LoginIndex",
  data() {
    return {
      loginForm: {
        userId: 'halo',
        password: '111'
      },
      rules: {
        userId: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入活动名称', trigger: 'blur'},
          {min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      // 校验参数
      this.$refs[formName].validate((valid) => {
        if (valid) {
          post('/doLogin', this.loginForm).then(res => {
            if (res && res.data) {
              console.log(res.data)
              this.$store.commit('setToken', res.data)
              this.$router.push('/');
            } else {
              this.$message.error(res.msg)
            }
          })
        } else {
          this.$message({
            type: 'error',
            message: '参数不合法'
          });
          return false;
        }
      });
    },
    // 事件
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$message({
        type: 'info',
        message: '已重置'
      });
    }
  }
}
</script>

<style scoped lang="less">
.login_container {
  background-color: #42b983;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    padding: 10px;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}

.btns {
  display: flex;
  justify-content: flex-end;

}

.login_in {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
